﻿
#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Reflection;
    using System.Text;

    using MarketPulse.BusinessLogic.Common;

#endregion

namespace MarketPulse.BusinessLogic.Private.StateMachine
{
    [Serializable]
    internal class VisitStateBase : IVisitState
    {
        /// <summary></summary>
        protected Guid mVisitStateType;

        /// <summary></summary>
        protected string mVisitStateString;

        protected bool mThrowIfInvalid;

        internal VisitStateBase(bool throwIfInvalid)
        {
            mThrowIfInvalid = throwIfInvalid;
        }

        protected void InitVisitStateString()
        {
            mVisitStateString = VisitStatusTypeList.GetList().GetItemByKey(mVisitStateType).Value;
        }

        private void ThrowTransitionException()
        {
            StackTrace stackTrace = new StackTrace();
            StackFrame stackFrame = stackTrace.GetFrame(1);
            MethodBase methodBase = stackFrame.GetMethod();
            throw new VisitStateTransitionException(mVisitStateString, methodBase.Name);
        }

        public virtual bool Assign(IVisitStateContext state, bool tryTransition, Guid userGuid, DateTime plannedDateTime)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool Unassign(IVisitStateContext state, bool tryTransition, Visit.PaymentStatus payment)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool Release(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool StartDataEntry(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool DataEnter(IVisitStateContext context, bool tryTransition, System.Xml.Linq.XElement responses)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool CompleteDataEntry(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool QAStartDataEntry(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool QADataEnter(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool QAApprove(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool QAFail(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool QAPutOnHold(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool QAReopen(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool MarkReadyForTransfer(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public virtual bool Revisit(IVisitStateContext context, bool tryTransition)
        {
            if (mThrowIfInvalid && !tryTransition)
            {
                ThrowTransitionException();
            }
            return false;
        }

        public Guid StateGuid
        {
            get
            {
                return mVisitStateType;
            }
        }

        public bool ThrowIfInvalid
        {
            get
            {
                return mThrowIfInvalid;
            }

            set
            {
                mThrowIfInvalid = value;
            }
        }
        

    }
}
